namespace CIS
{
    using CISLib;
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class InterfaceCompanySetPage : Page
    {
        protected DropDownList areaDdl;
        protected DropDownList catDdl;
        protected DataGrid CompanyListDg;
        protected Image iCompany;
        protected UserPrincipal Principal = null;
        protected Button searchBtn;
        protected TextBox searchTbx;

        private void areaDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.catDdl.Items.Clear();
            this.catDdl.DataSource = ManageCAT.GetCAT(new Guid(this.areaDdl.SelectedValue));
            this.catDdl.DataTextField = "CATGroupName";
            this.catDdl.DataValueField = "CATGroupId";
            this.catDdl.DataBind();
        }

        private void CompanyListDg_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string cmd = e.CommandName;
            string strCompanyId = e.CommandArgument.ToString();
            Guid companyId = new Guid();
            try
            {
                companyId = new Guid(strCompanyId);
            }
            catch (Exception)
            {
                return;
            }
            switch (cmd)
            {
                case "Edit":
                    base.Response.Redirect("SetInterface.aspx?CompanyId=" + companyId.ToString(), true);
                    break;

                case "GetCustomer":
                    try
                    {
                        if (base.Request.QueryString["Type"].ToString() == "ProjectInfo")
                        {
                            base.Response.Redirect("~/Project/ProjectInfoPage.aspx?CompanyId=" + companyId.ToString(), true);
                        }
                    }
                    catch
                    {
                    }
                    base.Response.Redirect("CompanyInfoBasicCustomerListPage.aspx?CompanyId=" + companyId.ToString(), true);
                    break;
            }
        }

        private void CompanyListDg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.CompanyListDg.CurrentPageIndex = e.NewPageIndex;
            this.CompanyListDgDataBind();
        }

        private void CompanyListDgDataBind()
        {
            Province[] provinceAry = this.Principal.Province;
            Area[] areaAry = this.Principal.OwnArea;
            Cat[] catAry = this.Principal.OwnCat;
            DataTable dataTable = new DataTable();
            try
            {
                if (base.Request.QueryString["Type"].ToString() == "ProjectInfo")
                {
                    this.searchTbx.Visible = false;
                    this.searchBtn.Visible = false;
                    int y = 0;
                    for (int i = 0; i < areaAry.Length; i++)
                    {
                        for (int x = 0; x < provinceAry.Length; x++)
                        {
                            for (int j = 0; j < catAry.Length; j++)
                            {
                                DataTable dt = ManageCompany.GetCompany(catAry[j].CatId, provinceAry[x].ProvinceId, areaAry[i].AreaId).Tables[0];
                                if (y == 0)
                                {
                                    dataTable = dt.Clone();
                                    y++;
                                }
                                foreach (DataRow tmpRow in dt.Rows)
                                {
                                    dataTable.ImportRow(tmpRow);
                                }
                            }
                        }
                    }
                    this.CompanyListDg.DataSource = dataTable;
                    this.CompanyListDg.DataBind();
                    this.CompanyListDg.Columns[4].Visible = false;
                    return;
                }
            }
            catch (Exception)
            {
            }
            int yy = 0;
            if (this.catDdl.SelectedItem.Text == "Root")
            {
                for (int i = 0; i < areaAry.Length; i++)
                {
                    DataTable dt = ManageCompany.GetCompany(areaAry[i].AreaId, this.searchTbx.Text.Trim()).Tables[0];
                    if (yy == 0)
                    {
                        dataTable = dt.Clone();
                        yy++;
                    }
                    foreach (DataRow tmpRow in dt.Rows)
                    {
                        dataTable.ImportRow(tmpRow);
                    }
                }
            }
            else
            {
                for (int i = 0; i < areaAry.Length; i++)
                {
                    DataTable dt = ManageCompany.GetCompany(areaAry[i].AreaId, this.searchTbx.Text.Trim(), new Guid(this.catDdl.SelectedValue)).Tables[0];
                    if (yy == 0)
                    {
                        dataTable = dt.Clone();
                        yy++;
                    }
                    foreach (DataRow tmpRow in dt.Rows)
                    {
                        dataTable.ImportRow(tmpRow);
                    }
                }
            }
            this.CompanyListDg.DataSource = dataTable;
            this.CompanyListDg.DataBind();
        }

        private void FillPage()
        {
            this.CompanyListDgDataBind();
        }

        protected string GetProvinceNameViaProvinceId(object provinceId)
        {
            Guid id = (Guid) provinceId;
            return ManageProvince.GetProvinceViaId(id).ProvinceName;
        }

        private void InitializeComponent()
        {
            this.areaDdl.SelectedIndexChanged += new EventHandler(this.areaDdl_SelectedIndexChanged);
            this.searchBtn.Click += new EventHandler(this.searchBtn_Click);
            this.CompanyListDg.ItemCommand += new DataGridCommandEventHandler(this.CompanyListDg_ItemCommand);
            this.CompanyListDg.PageIndexChanged += new DataGridPageChangedEventHandler(this.CompanyListDg_PageIndexChanged);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            Area[] areaAry = this.Principal.OwnArea;
            this.SetFromImage();
            this.SetFrom();
            this.CompanyListDg.PageSize = 14;
            this.areaDdl.Items.Clear();
            for (int i = 0; i < areaAry.Length; i++)
            {
                ListItem tmpListItem = new ListItem();
                tmpListItem.Text = areaAry[i].AreaName;
                tmpListItem.Value = areaAry[i].AreaId.ToString();
                this.areaDdl.Items.Add(tmpListItem);
            }
            this.catDdl.Items.Clear();
            this.catDdl.DataSource = ManageCAT.GetCAT(new Guid(this.areaDdl.SelectedValue));
            this.catDdl.DataTextField = "CATGroupName";
            this.catDdl.DataValueField = "CATGroupId";
            this.catDdl.DataBind();
        }

        private bool IsDelete()
        {
            if (this.Principal.OwnRole.TypeA == 0)
            {
                base.Response.Write("<script> alert('没有权限操作！') </script>");
                return false;
            }
            return (this.Principal.OwnRole.TypeA == 1);
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void searchBtn_Click(object sender, EventArgs e)
        {
            try
            {
                this.CompanyListDgDataBind();
            }
            catch (Exception)
            {
            }
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeA == 0)
            {
                this.InitPage();
                this.FillPage();
            }
            else if (this.Principal.OwnRole.TypeA == 1)
            {
                this.InitPage();
                this.FillPage();
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            this.CompanyListDg.Columns[0].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CompanyInfo", "name", language, path);
            this.CompanyListDg.Columns[1].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CompanyInfo", "add", language, path);
            this.CompanyListDg.Columns[2].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CompanyInfo", "tel", language, path);
            this.CompanyListDg.Columns[3].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CompanyInfo", "fax", language, path);
            this.CompanyListDg.Columns[4].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CompanyInfo", "edit", language, path);
            this.searchBtn.Text = ManageSystemConfig.GetSystemSetLanguage("CompanyInfo", "searchBtn", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iCompany.ImageUrl = "images/title_CompanyInfo.jpg";
                    break;

                case "en":
                    this.iCompany.ImageUrl = "images/title_CompanyInfoEn.jpg";
                    break;
            }
        }
    }
}

